package controller.frontOffice;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.model.UploadedFile;

import controller.backOffice.ClientController;

import ejb.author.AuthorService;
import ejb.book.BookService;
import ejb.order.OrderService;
import entities.Author;
import entities.Book;
import entities.Category;
import entities.Client;
import entities.Order;
import entities.OrderItem;

@Named
@RequestScoped
public class OrderController implements Serializable {

	@EJB
	private OrderService orderService;

	private List<Order> orders; 
	
	private transient @Inject OrderItem selectedOrderItem;
	
	private transient @Inject Order order; 
	
	private Order tempOrder; 
	
	public List<Order> getCurrentClientOrders() {
		FacesContext ctx = FacesContext.getCurrentInstance();
		ClientController cc = ctx.getApplication().evaluateExpressionGet(ctx, "#{clientController}", ClientController.class);
		if(cc.getCurrentClient() != null) {
			Map params = new HashMap<String, Client>();
			params.put("client", cc.getCurrentClient());
			return (List<Order>)orderService.findWithNamedQuery("Order.findAllByClientId", params);
		}
		return null;
	}
	
	public List<Order> getOrders() {
		return getCurrentClientOrders();
	}

	public void setOrders(List<Order> orders) {
		this.orders = orders;
	}
	
	public String createOrder() {

		FacesContext ctx = FacesContext.getCurrentInstance();
		ClientController cc = ctx.getApplication().evaluateExpressionGet(ctx, "#{clientController}", ClientController.class);
		
		Order orderToSave = ctx.getApplication().evaluateExpressionGet(ctx, "#{cartController.order}", Order.class);
		orderToSave.setDate(new Date());
		orderToSave.setClient(cc.getCurrentClient());
		orderToSave.setStatutPaiement(0);
		ctx.getApplication().evaluateExpressionGet(ctx, "#{cartController}", CartController.class).setOrder(new Order());
		orderService.create(orderToSave);
		return "clientOrders";
	}
	
	public Order getOrder() {
		return order;
	}
	
	public void setOrder(Order order) {
		tempOrder = order;
		this.order = order;
	}
	
	public OrderItem getSelectedOrderItem() {
		return selectedOrderItem;
	}
	
	public void setSelectedOrderItem(OrderItem orderItem) {
		this.selectedOrderItem = orderItem;
	}

	public Order getTempOrder() {
		return tempOrder;
	}

	public void setTempOrder(Order tempOrder) {
		this.tempOrder = tempOrder;
	}
	
	
	
}
